<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row type="flex" justify="space-between" align="middle">
          <!-- 目录名称那一行开始 -->
          <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline">
            <el-form-item label="目录名称" prop="directoryName">
              <el-input v-model="formInline.directoryName"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-select v-model="formInline.state" placeholder="请选择" clearable>
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clear">清除</el-button>
              <el-button size="small" type="primary" @click="hQuery">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-button type="success" icon="el-icon-edit" size="small" @click="addDirectory" class="addbutton">
            新增目录
          </el-button>
        </el-row>
        <div class="black"><i class="el-alert__icon el-icon-info"></i>数据一共{{ this.total }}条</div>
        <!-- 目录名称那一行结束 -->
        <!-- table列表 -->
        <el-table :data="list">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="subjectName" label="所属学科" />
          <el-table-column prop="directoryName" label="目录名称" />
          <el-table-column prop="username" label="创建者" />
          <el-table-column label="创建日期">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量" />
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 1">已启用</span>
              <span v-else>已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150">
            <template>
              <el-button type="text" size="small">禁用</el-button>
              <el-button type="text" size="small">修改</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :total="total"
            background
            :current-page="query.page"
            :page-size="query.size"
            :page-sizes="[10, 5, 20, 2]"
            @current-change="changePage"
            @size-change="changeSize"
            layout="prev, pager, next,sizes"
          />
        </el-row>
      </el-card>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="所属科目" prop="directoryName">
            <el-select v-model="form.directoryName" placeholder="请选择活动区域" style="width: 100%">
              <el-option
                v-for="item in subjectName"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" label-width="80px" prop="subjectID">
            <el-input v-model="form.subjectID"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, add } from '@/api/hmmm/directorys.js'
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
export default {
  name: 'directory',
  data() {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      subjectName: [],
      rules: {
        /* subjectID: [
          { required: true, message: '请选择所属学科', trigger: ['blur', 'change'] }
        ], */
        subjectID: [{ required: true, message: '请输入目录名称', trigger: ['blur', 'change'] }]
      },
      formInline: {
        directoryName: '',
        state: ''
      },
      dialogVisible: false,
      list: [],
      query: {
        page: 1, // 当前页
        size: 10 // 每页条数
      },
      total: 0 // 总数
    }
  },
  created() {
    getCompanys()
    getQuestion()
    this.getList()
  },
  methods: {
    // str='默认值' str如果不传 读取默认值'YYYY-MM-DD'
    formatDate(value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    },
    mlParams() {
      const formInline = {}
      const { directoryName, state } = this.formInline
      const { page, size } = this.query
      formInline.page = page || null
      formInline.pagesize = size || null
      formInline.directoryName = directoryName || null
      formInline.state = state || null
      return formInline
    },
    // 获取员工列表
    async getList() {
      const formInline = this.mlParams()
      const { data } = await list(formInline)
      this.list = data.items
      this.total = data.counts
      console.log(data)
    },
    // 搜索
    hQuery() {
      this.query.page = 1
      this.getList()
    },
    // 清除
    clear() {
      this.$refs.formInline.resetFields()
      this.query.page = 1
      this.query.size = 10
      this.getList()
    },
    // 页码切换
    changePage(page) {
      console.log(page)
      this.query.page = page
      // 根据新页码请求数据=》刷列表
      /*
      1.更新页码
      2.刷列表
      */
      this.getList()
    },
    // 每页显示条数切换
    changeSize(size) {
      console.log(size)
      /*
      1.更新每页条数
      2.刷列表
      */
      this.query.size = size
      this.getList()
    },
    async addDirectory() {
      this.dialogVisible = true
      const { data } = await simple()
      this.subjectName = data
      console.log(data)
    },
    async add() {
      await add(this.form)
      this.$message.success('添加成功!')
    }
  }
}
</script>
<style scoped>
.black {
  height: 36px;
  background-color: #f4f4f5;
  font-size: 13px;
  line-height: 36px;
  border-radius: 5px;
  padding-left: 20px;
  color: #909399;
}
.addbutton {
  margin-left: 460px;
}
</style>
